package com.htsc.mrp.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author huqi
 * @date 2019/3/18 14:28
 * @description: Hydsoft.com Inc. Copyright (c) 2004-2018 All Rights Reserved.
 * 修改xml文件
 */
public class UpdateXmlUtil {
    public static void updateMapperXML(
            String mapperAddress,
            String dbTableName
    ) throws DocumentException, IOException {
        String modelName = StringUtil.lineToHump(dbTableName);
        Document doc = new SAXReader().read(new File(mapperAddress.replace("\\", "/") + "/" + modelName + "Mapper.xml"));

        /**
         * 修改属性值
         * 方法：使用Attribute类(属性类)的setValue()方法
         * 1.得到标签对象
         * 2.得到属性对象
         * 3.修改属性值
         */
        //1.得到标签对象
        Element stuElem = doc.getRootElement();
        //2.通过增加同名属性的方法，修改属性值
        stuElem.addAttribute("namespace", "I"+modelName+"Dao");
        //指定文件输出的位置
        FileOutputStream out = new FileOutputStream(mapperAddress.replace("\\", "/") + "/" + modelName + "Mapper.xml");
        // 指定文本的写出的格式：
        OutputFormat format = OutputFormat.createPrettyPrint();   //漂亮格式：有空格换行
        format.setEncoding("UTF-8");
        //1.创建写出对象
        XMLWriter writer = new XMLWriter(out, format);
        //2.写出Document对象
        writer.write(doc);
        //3.关闭流
        writer.close();
    }

    public static void updateGeneratorXML(
            String dbName,
            String dbPassword,
            String dbTableName,
            String targetProject,
            String targetResources,
            String modelTargetPackage,
            String daoTargetPackage,
            String mapperTargetPackage,
            String jdbcUrl
    ) throws DocumentException, IOException {
        Document doc = new SAXReader().read(new File("src/main/resources/generator/generatorConfig.xml"));

        /**
         * 修改属性值
         * 方法：使用Attribute类(属性类)的setValue()方法
         * 1.得到标签对象
         * 2.得到属性对象
         * 3.修改属性值
         */
        //得到标签对象
        Element jdbcElem = doc.getRootElement().element("context").element("jdbcConnection");
        //得到属性对象,修改属性值
        //修改数据相关信息
        jdbcElem.attribute("connectionURL").setText(jdbcUrl);
        jdbcElem.attribute("userId").setText(dbName);
        jdbcElem.attribute("password").setText(dbPassword);

        //修改生成po类的相关信息
        Element javaModelElem = doc.getRootElement().element("context").element("javaModelGenerator");
        javaModelElem.attribute("targetPackage").setText(modelTargetPackage);
        javaModelElem.attribute("targetProject").setText(targetProject);
        //修改baseEntity的位置信息
        Element property = (Element) javaModelElem.elements("property").get(2);
        property.attribute("value").setText(modelTargetPackage.substring(0,modelTargetPackage.indexOf("po"))+"base.BaseEntity");

        Element sqlMapGeneratorElem = doc.getRootElement().element("context").element("sqlMapGenerator");
        sqlMapGeneratorElem.attribute("targetPackage").setText(mapperTargetPackage);
        sqlMapGeneratorElem.attribute("targetProject").setText(targetResources);

        Element javaClientGeneratorElem = doc.getRootElement().element("context").element("javaClientGenerator");
        javaClientGeneratorElem.attribute("targetPackage").setText(daoTargetPackage);
        javaClientGeneratorElem.attribute("targetProject").setText(targetProject);

        Element tableElem = doc.getRootElement().element("context").element("table");
        tableElem.attribute("tableName").setText(dbTableName);


        //指定文件输出的位置
        FileOutputStream out = new FileOutputStream("src/main/resources/generator/generatorConfig.xml");
        // 指定文本的写出的格式：
        OutputFormat format=OutputFormat.createPrettyPrint();   //漂亮格式：有空格换行
        format.setEncoding("UTF-8");
        //1.创建写出对象
        XMLWriter writer=new XMLWriter(out,format);
        //2.写出Document对象
        writer.write(doc);
        //3.关闭流
        writer.close();
    }
}
